package com.example.shuiyin.controller;

import com.example.shuiyin.dto.ApiResponse;
import com.example.shuiyin.dto.PlanDto;
import com.example.shuiyin.dto.SubscribeRequest;
import com.example.shuiyin.dto.SubscriptionStatusDto;
import com.example.shuiyin.service.SubscriptionService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/subscription")
@Slf4j
@RequiredArgsConstructor
public class SubscriptionController {

    private final SubscriptionService subscriptionService;

    /**
     * 获取可用套餐
     */
    @GetMapping("/plans")
    public ResponseEntity<ApiResponse<List<PlanDto>>> getAvailablePlans() {
        List<PlanDto> plans = subscriptionService.getAvailablePlans();
        return ResponseEntity.ok(ApiResponse.success("获取套餐列表成功", plans));
    }

    /**
     * 订阅套餐
     */
    @PostMapping("/subscribe")
    public ResponseEntity<ApiResponse<String>> subscribe(
            @Valid @RequestBody SubscribeRequest request,
            @RequestAttribute("userId") Long userId) {
        log.info("用户[{}]订阅请求: {}", userId, request);
        String orderId = subscriptionService.subscribe(userId, request);
        return ResponseEntity.ok(ApiResponse.success("订阅请求已提交", orderId));
    }

    /**
     * 获取当前订阅状态
     */
    @GetMapping("/subscription/status")
    public ResponseEntity<ApiResponse<SubscriptionStatusDto>> getSubscriptionStatus(
            @RequestAttribute("userId") Long userId) {
        log.info("用户[{}]查询订阅状态", userId);
        SubscriptionStatusDto status = subscriptionService.getSubscriptionStatus(userId);
        return ResponseEntity.ok(ApiResponse.success("获取订阅状态成功", status));
    }

    /**
     * 取消订阅
     */
    @PostMapping("/cancel")
    public ResponseEntity<ApiResponse<Void>> cancelSubscription(
            @RequestAttribute("userId") Long userId) {
        log.info("用户[{}]取消订阅", userId);
        subscriptionService.cancelSubscription(userId);
        return ResponseEntity.ok(ApiResponse.success("订阅已成功取消"));
    }
} 